package gui;

import java.awt.*;
import java.awt.event.*;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.swing.*;

import model.Customer;
import database.CustomerOperations;
import database.EstateOperations;
import database.RoadOperations;

public class addOrderGUI extends JFrame implements ActionListener,ItemListener 
{
	private static final long serialVersionUID = 1L;
	private JPanel p1, p2, p3;
	private JLabel l1,l3,l4,l5,l6,l7,l8,l9;
	private JTextField f1,f2,f3,f4,f5;
	private JButton next, cancel;
	private JComboBox<String> est, road1;
	private EstateOperations eo;
	private RoadOperations ro;
	private CustomerOperations co;
	private ArrayList<String> ests;
	private ArrayList<String> rds;
	private String estName;
	private String roadName;
	
	public addOrderGUI() 
	{
		super("Add Order Screen");
		this.setSize(840, 500);
		this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		this.setLayout(new FlowLayout());
		this.setLocationRelativeTo(null);
		this.setResizable(false);
		eo=new EstateOperations();
		ro=new RoadOperations();
		
		//P1
		p1 = new JPanel();
		p1.setPreferredSize(new Dimension(840, 100));
		l1 = new JLabel("Fill in details and press next to continue, press cancel to leave page without saving.");
		p1.add(Box.createRigidArea(new Dimension(0,50)));
		p1.add(l1);
		p1.add(Box.createRigidArea(new Dimension(0,50)));
		
		//P2
		p2 = new JPanel(new GridLayout(7, 1));
		p2.setPreferredSize(new Dimension(300, 190));
		l3 = new JLabel("Firstname: ");
		l4 = new JLabel("Surname: ");
		l5 = new JLabel("Phone number: ");
		l6 = new JLabel("E-mail: ");
		l7 = new JLabel("House number: ");
		l8 = new JLabel("Road: ");
		l9 = new JLabel("Estate: ");
		
		//list of estates in combo box
		est = new JComboBox<String>();	
		est.setPreferredSize(new Dimension(100,25));
		ests=new ArrayList<String>();
		ests=eo.getEstateNames();
		for(int i=0;i<ests.size();i++)
		{
			est.addItem(ests.get(i));
		}
		est.addItemListener(this);
		estName=(String)est.getSelectedItem();
				
		//list of roads in combo box
		road1 = new JComboBox<String>();	
		road1.setPreferredSize(new Dimension(100,25));
		rds=new ArrayList<String>();
		rds=ro.getRoadNames(estName);
		for(int i=0;i<rds.size();i++)
		{
			road1.addItem(rds.get(i));
		}
		road1.addItemListener(this);
		roadName=(String)road1.getSelectedItem();
				
		f1 = new JTextField(15);
		f2 = new JTextField(15);
		f3 = new JTextField(15);
		f4 = new JTextField(15);
		f5 = new JTextField(15);
		
		p2.add(l3);
		p2.add(f1);
		p2.add(l4);
		p2.add(f2);
		p2.add(l5);
		p2.add(f3);
		p2.add(l6);
		p2.add(f4);
		p2.add(l7);
		p2.add(f5);
		p2.add(l9);
		p2.add(est);
		p2.add(l8);
		p2.add(road1);
		
		
		//P3
		
		p3 = new JPanel();
		p3.setPreferredSize(new Dimension(840 ,100));
		next = new JButton("NEXT");
		cancel = new JButton("CANCEL");
		
		p3.add(cancel);
		cancel.addActionListener(this);
		p3.add(Box.createRigidArea(new Dimension(150,110)));
		p3.add(next);
		next.addActionListener(this);
		
		//Adding Panels
		this.add(p1, BorderLayout.NORTH);
		this.add(p2, BorderLayout.CENTER);
		this.add(p3, BorderLayout.PAGE_END);
		//this.add(p4, BorderLayout.NORTH);
		
	}
	
	public static void main(String args[]) {
		addOrderGUI ao = new addOrderGUI();
		ao.setVisible(true);
	}

	//check fields filled in
	public boolean check()
	{
		co=new CustomerOperations();
		boolean well=false;
		if(f1.getText().equals("")||f2.getText().equals("")||f5.getText().equals("")||f3.getText().equals(""))
		{
			JOptionPane.showMessageDialog(null, "You must enter data in the names, phone and house number fields","ERROR",JOptionPane.WARNING_MESSAGE);
		}
		else if(co.checkAddress(f5.getText(),getRoadId())==true)
		{
			JOptionPane.showMessageDialog(null, "The address you have choosen is used by an excisting customer","ERROR",JOptionPane.WARNING_MESSAGE);
		}
		else 
		{
			well=true;
		}
		try
		{
			co.closeDB();
		}
		catch(NullPointerException e)
		{
			
		}
		
		return well;
	}
	
	//collect customer data
	public Customer collect()
	{
		String fname=f1.getText();
		String lname=f2.getText();
		int phone=Integer.parseInt(f3.getText());
		String email=f4.getText();
		String house=f5.getText();
		int roadId=getRoadId();
		Customer c = new Customer(fname,lname,email,phone,house,roadId);
		return c;
	}
	
	//get road id
	public int getRoadId()
	{
		int a=ro.getEstId(estName);
		int id=ro.getRoadId(a,roadName);
		return id;
	}
	
	@Override
	public void actionPerformed(ActionEvent ae) 
	{
		if(ae.getSource()==cancel)
		{
			dispose();
		}
		if(ae.getSource()==next)
		{
			boolean ok=check();
			if(ok==true)
			{
				productInputScreen pis=new productInputScreen(collect());
				pis.setVisible(true);
				this.setVisible(false);
			}
		}
		
	}

	@Override
	public void itemStateChanged(ItemEvent ae) 
	{
		if (ae.getSource() == est && ae.getStateChange() == ItemEvent.SELECTED)
		{
			estName=(String)est.getSelectedItem();
			rds=ro.getRoadNames(estName);
			road1.removeAllItems();
			for(int i=0;i<rds.size();i++)
			{
				road1.addItem(rds.get(i));
			}
		}
		if (ae.getSource() == road1 && ae.getStateChange() == ItemEvent.SELECTED)
		{
			roadName=(String)road1.getSelectedItem();
		}
		
	}

}



